<?php

namespace app\admin\controller;


use app\common\model\ActiveJoin;

class Active extends Base
{

    public function __construct()
    {
        parent::__construct();
    }


    /**
     * 活动列表
     * @return \think\response\Json
     */
    public function active_list()
    {
        $where = array();
        if (input('id', ''))
            $where['id'] = input('id');
        if (input('title', ''))
            $where['title'] = array('like', "%" . input('title', '') . "%");
        if (input('type', ''))
            $where['type'] = input('type');
        if (input('is_start', ''))
            $where['is_start'] = input('is_start');
        if (input('start', ''))
            $where['start'] = array('between', input('start'));
        if (input('end', ''))
            $where['end'] = array('between', input('end'));
        $page = max(input('page'), 1);
        $size = 20;
        $list = \app\common\model\Active::getPage($where, $page, $size);
        $total = \app\common\model\Active::getCount($where);
        if (!$list) {
            $list = [];
            return returnJson(0, '', compact('list', 'total', 'page', 'size'));
        }
        foreach ($list as $k => $v) {
            $list[$k]['source'] = $v['source'];
        }
        return returnJson(0, '', [
            'list' => $list,
            'size' => $size,
            'total' => $total,
            'page' => $page
        ]);
    }

    /**
     * 编辑活动
     * @return \think\response\Json
     */
    public function edit_active()
    {
        if (request()->isPost()) {
            $input = input();
            if (!isset($input['title'])) {
                return returnJson(1, '请填写完整');
            }
            $data = array(
                'title' => $input['title'],
                'content' => $input['content'],
                'join_cycle' => $input['join_cycle'],
                'type' => $input['type'],
                'is_start' => $input['is_start'],
                'start' => $input['start'],
                'end' => $input['end'],
                'js_type' => $input['js_type'],
                'source' => $input['source'],
                'sort' => $input['sort'],
                'source_type' => $input['source_type'],
                'create_time' => time(),
                'update_time' => time(),
            );
            if ($input['id']) {
                $data['id'] = $input['id'];
            }
            if ($data['id']) {
                $res = \app\common\model\Active::update($data, ['id' => $data['id']]);
            } else {
                $res = \app\common\model\Active::create($data);
            }
            if ($res) {
                return returnJson(0, '操作成功');
            }
            return returnJson(1, '网络异常，请重试');
        }
    }

    /**
     * 删除活动
     * @return \think\response\Json
     */
    public function delete()
    {
        $input = input();
        if (!$input['id']) {
            return returnJson(1, 'error');
        }
        $active = \app\common\model\Active::getOne(['id' => $input['id']]);
        if (!$active) {
            return returnJson(1, '活动不存在');
        }
        $res = \app\common\model\Active::destroy($input['id']);
        if ($res) {
            model('qiniu')->remove($active['source']);
            return returnJson(0, '已删除');
        } else {
            return returnJson(1, '删除失败，请重试');
        }
    }


    /**
     * 所有活动列表
     * @return \think\response\Json
     */
    public function active_all_list()
    {
        $where = [];
        $list = \app\common\model\Active::getAll($where, 'id desc', 'id,title');
        if (!$list) {
            $list = [];
        }
        return returnJson(0, '', $list);
    }


    /**
     * 参与活动列表
     * @return \think\response\Json
     */
    public function join_list()
    {
        $where = array();
        if (input('uid', ''))
            $where['uid'] = input('uid');
        if (input('aid', ''))
            $where['aid'] = input('aid');
        if (input('is_pingfen', ''))
            $where['is_pingfen'] = input('is_pingfen');
        $page = max(input('page'), 1);
        $size = 20;
        $list = \app\common\model\ActiveJoin::getPage($where, $page, $size);
        $total = \app\common\model\ActiveJoin::getCount($where);
        if (!$list) {
            $list = [];
            return returnJson(0, '', compact('list', 'total', 'page', 'size'));
        }
        $active = \app\common\model\Active::getColumn([], 'id,title');
        foreach ($list as $k => $v) {
            $list[$k]['source'] = $v['source'];
            $list[$k]['title'] = isset($active[$v['aid']]) ? $active[$v['aid']] : '';
            $list[$k]['nickname'] = \app\common\model\User::getValue(['id' => $v['uid']], 'nickname');
            $list[$k]['s_name'] = \app\common\model\Student::getValue(['id'=>$v['sid']],'name');
        }
        return returnJson(0, '', [
            'list' => $list,
            'size' => $size,
            'total' => $total,
            'page' => $page
        ]);
    }

    /**
     * 评分活动
     * @return \think\response\Json
     */
    public function join_score()
    {
        if (request()->isPost()) {
            $input = input();
            if (!isset($input['id'])) {
                return returnJson(1, '参与信息错误');
            }  else {
                $active_join = ActiveJoin::getOne(['id'=>$input['id']]);
                if(!$active_join){
                    return returnJson(1, '参与信息不存在');
                }
            }
            if (!isset($input['score'])) {
                return returnJson(1, '参与信息错误');
            }
            $active = \app\common\model\Active::getOne(['id'=>$active_join['aid']]);
            $data = array(
                'is_pingfen' => 1,
                'score' => input('score', 0),
                'update_time' => time()
            );
            if ($input['id']) {
                $data['id'] = $input['id'];
            }
            $res = \app\common\model\ActiveJoin::update($data, ['id' => $input['id']]);
            \app\common\model\User::changeMoney($active_join['uid'],2,$data['score'],'参与活动"'.$active['title'].'"奖励积分',$active_join,[
                'manager_name' => $this->manager['name'],
            ]);
            if ($res) {
                return returnJson(0, '操作成功');
            }
            return returnJson(1, '网络异常，请重试');
        }
    }


}

